# -*- coding:utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _

import datetime
#from dateutil.relativedelta import relativedelta

class Contract(models.Model):

    _inherit = 'hr.contract'

    tax_deduction = fields.Monetary('Tax Deduction', digits=(16, 2), help="Employee's monthly tax deduction")

class HrEmployee(models.Model):
    _inherit = 'hr.employee'

    @api.multi
    def accumulate(self, payslip,codes, sign=1):
        codes = isinstance(codes, list) and codes or [codes]
        
        date = payslip.date_from
        year_first_date = datetime.date(date.year, 1, 1)
        date_from = date
        
        old_slip_ids = self.slip_ids.filtered(
            lambda item: ( item.date_from >= year_first_date
                     ) and item.date_from < date_from )
        
        slip_lines = old_slip_ids.mapped('line_ids').filtered(
            lambda item: item.category_id.code in codes)
        
        ret = sign * sum(slip_lines.mapped('total'))
        print (codes, ret)
        return ret

class HrPayslipLine(models.Model):
    _inherit = 'hr.payslip.line'
    _order = 'contract_id, appears_on_payslip desc, sequence'

